<?php

namespace App\Models;

use App\Enums\GoodsBusinessEnum;
use App\Services\CommonService;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class OrderGoods extends Model
{
    use HasFactory;
    protected $table = 'sp_order_goods_2';
    protected $primaryKey = 'id';
    public $timestamps = false;

    //类型
    const PACT_TYPE_RENT_AND_RETURN = 0;//随租随还
    const PACT_TYPE_FIXED_LEASE_TERM = 1;//固定租期
    const PACT_TYPE_RETURN_OUT = 2;//租完即送

    //租期设置。''1''按月，''2''按天' 3自定义,
    const LEASE_SET_MONTH = 1;//按月
    const LEASE_SET_DAY = 2;//按天
    const LEASE_SET_DIY = 3;//自定义

    /**
     * 编辑订单数据
     */
    protected function editOrderData($request){
        $num = $request['num'];
        $orderId = $request['orderId'];
        $cash_pledge = $request['cash_pledge'];
        $data = [];
        if($num == 2){
            $data['pact_type'] = $request['pact_type'];
            $data['cash_pledge'] = $cash_pledge;
        }

        $this->where('order_id',$orderId)->update($data);

    }

    public function CalRent($goods, $sku_meal_info)
    {
        $lease_price =$goods['business_type']==GoodsBusinessEnum::LEASE()->getKey()? $sku_meal_info['month_price']:$sku_meal_info['day_price'];
        $lease_time = $sku_meal_info['goods_rent_meal_value']['meal_value'];

        $rent_total = bcmul($lease_price, $lease_time,2); // '总租金',
        $month_total = $lease_time; // '总租期数',
        $every_month=$lease_price;

        $discount_amount = 0;
        $discount_type = $goods->discount_type;
        $discount_name = $goods->discount_name;

        switch ($discount_type) {
            case 1:// 首月金额
                $first_rent = $goods['discount_first_amount'];
                $every_month = sprintf("%.2f", ($rent_total - $first_rent) / ($month_total - 1));
                break;
            case 2:// 首月折扣
                if ($goods['discount_first'] < 0 || $goods['discount_first'] > 10) {
                    return ReturnMsg(CommonService::HTTP_CODE_FAIL, '首月折扣设置错误');
                }
                $first_rent = sprintf("%.2f", $every_month * $goods['discount_first'] / 10);
                $every_month = sprintf("%.2f", ($rent_total - $first_rent) / ($month_total - 1));
                break;
            case 3:
                // 计算满减
                if ($rent_total > $goods['discount_max_amount']) {
                    $rent_total = $rent_total - $goods['discount_max_sub_amount'];
                    $discount_amount = $goods['discount_max_sub_amount'];
                    $every_month = sprintf("%.2f", $rent_total / $month_total);
                    $first_rent = $every_month;
                } else {
                    $first_rent = $every_month;
                }
                break;
            default:
                $first_rent = $every_month;
        }
        $order = [
            'rent_total' => $rent_total,//  '总租金',
            'month_total' => $month_total,//总租期数

            'every_month' => $every_month,
            'first_rent' => $first_rent,

            'discount_type' => $discount_type,
            'discount_amount' => $discount_amount,
            'discount_name' => $discount_name,
        ];

        $order['remaining_num'] = $month_total - 1;
        $order['remaining_rent'] = $rent_total - $first_rent;
        if ($order['remaining_num'] <= 0) {
            $order['remaining_every_moth'] = 0;
        } else {
            $order['remaining_every_moth'] = sprintf("%.2f", $order['remaining_rent'] / $order['remaining_num']);
        }

        // ===== 默认无优惠end
        $order['last_num'] = 0;
        $order['last_rent'] = sprintf("%.2f", $rent_total - $every_month * ($order['remaining_num'] - 1) - $first_rent);
        if ($order['last_rent'] != $every_month) {
            $order['last_num'] = 1;
            $order['remaining_num'] = $order['remaining_num'] - 1;
        }

        return $order;
    }

}
